var express = require('express')
    , httpProxy = require('http-proxy')
    , proxyServer = httpProxy.createProxyServer()
    ;

// entryApp [
(function (port, hostname) {

    var entryApp = express();

    entryApp.use(function (req, res, next) {
        if (req.hostname === 'app2.qwer.ink') {
            proxyServer.web(req, res, { target: 'http://qwer.ink:8082' });
        } else if (req.headers.host === 'app3.qwer.ink') {
            proxyServer.web(req, res, { target: 'http://qwer.ink:8083' });
        } else {
            next();
        }
    });

    entryApp.get('/', function (req, res, next) {
        res.send('hello entryApp.');
    });

    entryApp.listen(port, hostname, function () {
        console.log('entryApp listening at http://%s:%s', hostname, port);
    });

})(80, '172.26.74.127');
// entryApp ]


// app2 [
(function (port, hostname) {

    var app2 = express();

    app2.get('/', function (req, res, next) {
        res.send('hello app2.');
    });

    app2.listen(port, hostname, function () {
        console.log('app2 listening at http://%s:%s', hostname, port);
    });

})(8082, '172.26.74.127');
// app2 ]


// app3 [
(function (port, hostname) {

    var app3 = express();

    app3.get('/', function (req, res, next) {
        res.send('hello app3.');
    });

    app3.listen(port, hostname, function () {
        console.log('app3 listening at http://%s:%s', hostname, port);
    });

})(8083, '172.26.74.127');
// app3 ]